﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Liger.Data;
using Liger.Model;
using Liger.Data.Linq;

namespace Test
{
    public class NorthwindLinqTest : BaseTest
    {
        public NorthwindLinqTest(DbContext db)
            : base(db)
        {
        }
        public override bool MethodEnabled(string name)
        {
            return name.StartsWith("TestSelectNew");
        }




        /// <summary>
        /// 测试 linq where(来自MS自带例子)
        /// 此示例使用 WHERE 筛选在伦敦的客户。
        /// </summary>
        public void TestLinqToSqlWhere01()
        {
            var q =
                from c in Db.Query<Customers>()
                where c.City == "London"
                select c;
            this.AssertReturn(q.ToList() != null);
        }


        /// <summary>
        /// 测试 linq where(来自MS自带例子)
        /// 此示例使用 WHERE 筛选1994 年或之后雇用的雇员。
        /// </summary>
        public void TestLinqToSqlWhere02()
        {
            var q =
                from e in Db.Query<Employees>()
                where e.HireDate >= new DateTime(1994, 1, 1)
                select e;
 
            this.AssertReturn(q.ToList() != null);
        }




        



        string ProParm { get { return "VINET"; } }
        public void TestWhereProParm()
        {
            var result = Db.Query<Orders>()
                .Where(c => c.CustomerID == ProParm)
                .ToList();

            this.AssertReturn(result != null);
        }


        public void TestWhereFieldParm()
        {
            var outerParm = "变量值";
            var result = Db.Query<Orders>()
                .Where(c => c.CustomerID == outerParm)
                .ToList();

            this.AssertReturn(result != null);
        }


        public void TestWhereDateTimeNow()
        {
            var result = Db.Query<Orders>()
                .Where(c => c.OrderDate == DateTime.Now)
                .ToList();

            this.AssertReturn(result != null);
        }

        public void TestWhereDateTime()
        {
            var result = Db.Query<Orders>()
                .Where(c => c.OrderDate == new DateTime(2011, 1, 1))
                .ToList();

            this.AssertReturn(result != null);
        }

        public void TestSelectNew()
        {
            var a = new
            {
                A = "BB",
                B = "CC"
            };

            var result = Db.Query<Orders>()
                .Select(c => new
                {
                    CustomerID = c.CustomerID,
                    EmployeeID = "字画i"
                }).ToList();

            this.AssertReturn(result != null);
        }

        public void TestSelectNew2()
        {
            var result = Db.Query<Orders>()
                .Select(c => c.CustomerID).ToList();

            this.AssertReturn(result != null);
        }



        /// <summary>
        /// 测试 first带where
        /// </summary>
        public void TestFirstWithWhere()
        {
            var result = Db.Query<Orders>()
                .First(c => c.CustomerID.StartsWith("A"));

            this.AssertReturn(result != null);
        }


        public void TestGroupBy()
        {
            var result = Db.Query<Orders>()
                .GroupBy(c => c.CustomerID).ToList();

            this.AssertReturn(result != null);
        }


        public void TestTake()
        {
            var result = Db.Query<Orders>().Take(5).First();

            this.AssertReturn(result != null);
        }

        public void TestOrderBy()
        {
            var result = Db.Query<Orders>()
                .OrderBy(c => c.CustomerID)
                .ToList();

            this.AssertReturn(result != null);
        }

        public void TestOrderBy2()
        {
            var result = Db.Query<Orders>()
                .OrderByDescending(c => c.CustomerID)
                .ToList();

            this.AssertReturn(result != null);
        }

        public void TestThenBy()
        {
            var result = Db.Query<Orders>()
                .OrderBy(c => c.CustomerID)
                .ThenBy(c => c.EmployeeID)
                .ToList();

            this.AssertReturn(result != null);
        }

        //public void TestJoin()
        //{ 
        //    var result =
        //        (from o in Db.Query<Orders>()
        //         from c in Db.Query<Customers>()
        //         where o.CustomerID == c.CustomerID
        //         select new { o.CustomerID, c.ContactTitle }
        //        ).ToList();

        //    this.AssertReturn(result != null);
        //}


        public void TestAny()
        {
            var result = Db.Query<Categories>()
                .Where(c => c.CategoryName == "Condiments")
                .Any(); 
            this.AssertReturn(result == true);
        }

        public void TestContains()
        { 
            string[] customerIDs = new string[] { "Around the Horn", "Consolidated Holdings" };
            var result = Db.Query<Customers>()
                .Where(c => customerIDs.Contains(c.CustomerID))
                .First();
            this.AssertReturn(result != null);
        }


        public void TestContainsInt()
        {
            int[] ids = new int[] {1,2,3,4};
            var result = Db.Query<Categories>()
                .Where(c => ids.Contains(c.CategoryID))
                .ToList();
            this.AssertReturn(result != null);
        }

        public void TestNotContains()
        { 
            string[] customerIDs = new string[] { "Around the Horn", "Consolidated Holdings" };
            var result = Db.Query<Customers>()
                .Where(c => !customerIDs.Contains(c.CustomerID))
                .ToList();
            this.AssertReturn(result != null);
        }

        public void TestStartsWith()
        { 
            var result = Db.Query<Customers>()
                .Where(c => c.CustomerID.StartsWith("Around"))
                .ToList();
            this.AssertReturn(result != null);
        }

        public void TestEndsWith()
        {
            var result = Db.Query<Customers>()
                .Where(c => c.CustomerID.EndsWith("Horn"))
                .ToList();
            this.AssertReturn(result != null);
        }
    }
}
